﻿@{
    ViewBag.Title = "用户等级管理";
}
@section headerSection{
    <style>
        #exampleToolbar > .form-group {
            float: left;
            margin: 0;
            margin-left: 5px;
        }
    </style>
}
<div class="animated fadeInRight">
    <!-- Panel Other -->
    <div class="ibox float-e-margins" style="margin-bottom: 0px;">
        <div class="ibox-content" style="padding: 5px 20px 0px 20px;">
            <div class="row row-lg">
                <div class="col-sm-12 col-sm-leftRight">
                    <!-- Example Toolbar -->
                    <div class="example">
                        <div class="btn-group hidden-xs" id="exampleToolbar" role="group">
                            <button id="userDetail" type="button" class="btn btn-outline btn-primary">
                                <i class="glyphicon glyphicon-th-large" aria-hidden="true">详细</i>
                            </button>
                            <button id="userAdd" type="button" class="btn btn-outline btn-primary">
                                <i class="glyphicon glyphicon-plus" aria-hidden="true">添加</i>
                            </button>
                            <button id="userEdit" type="button" class="btn btn-outline btn-primary">
                                <i class="glyphicon glyphicon-pencil" aria-hidden="true">编辑</i>
                            </button>
                            <button id="userDel" type="button" class="btn btn-outline btn-danger">
                                <i class="glyphicon glyphicon-trash" aria-hidden="true">删除</i>
                            </button>                          
                            <div class="form-group" style="margin-left:0;width:120px;">
                                <input id="minScore" name="minScore" type="text" class="form-control" placeholder="最低销售额">
                            </div>
                            <div class="form-group" style="width:120px;">
                                <input id="maxScore" name="maxScore" type="text" class="form-control" placeholder="最高销售额">
                            </div>
                            <div class="form-group" style="width:120px;">
                                <input id="minLine" name="minLine" type="text" class="form-control" placeholder="最低下限人数">
                            </div>
                            <div class="form-group" style="width:120px;">
                                <input id="maxLine" name="maxLine" type="text" class="form-control" placeholder="最高下限人数">
                            </div>
                            <div class="form-group" style="margin-top:-2px;width:120px;">
                                <select id="userLevel" name="userLevel" title="用户等级" class="form-control show-tick selectpicker" data-live-search="true" placeholder="用户等级">
                                    <option value="0" selected>全部</option>
                                </select>
                            </div>                         
                            <div class="form-group">
                                <button id="btnSearch" class="btn btn-primary" type="button"><i class="fa fa-search"></i>&nbsp;查询</button>
                            </div>
                        </div>
                        <table id="exampleTableToolbar" data-mobile-responsive="true" data-radio="true"></table>
                    </div>
                    <!-- End Example Toolbar -->
                </div>
            </div>
        </div>
    </div>
    <!-- End Panel Other -->
</div>
<!--  添加编辑用户等级 -->
<div id="sellForm" style="width: 580px; overflow: hidden; display: none;">
    <form class="form-horizontal m-t" id="sellFormTable" method="post">
        <div class="form-group">
            <label class="col-sm-3 control-label">用户等级<span class="glyphicon glyphicon-asterisk requiredtitle"></span>：</label>
            <div class="col-sm-8">
                <select id="user_level" name="user_level" title="请选择用户等级" class="form-control input-sm selectpicker" data-live-search="true" required="" aria-required="true" placeholder="请选择用户等级" style="padding: 5px 10px;">
                </select>
                <input type="hidden" id="id" name="id" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">销售额最小值：</label>
            <div class="col-sm-8">
                <input id="min_sale" name="min_sale" type="text" autoComplete="off" class="form-control input-sm" placeholder="请输入销售额最小值" title="请输入销售额最小值" />
            </div>
        </div>
        <div class="form-group" id="markPass">
            <label class="col-sm-3 control-label">销售额最大值：</label>
            <div class="col-sm-8">
                <input id="max_sale" name="max_sale" type="text" autoComplete="off" class="form-control input-sm" placeholder="请输入销售额最大值" title="请输入销售额最大值" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">下线人数最小值：</label>
            <div class="col-sm-8">
                <input id="min_line" name="min_line" type="text" autoComplete="off" class="form-control input-sm" placeholder="请输入下线人数最小值" title="请输入下线人数最小值" />
            </div>
        </div>
        <div class="form-group" id="markPass">
            <label class="col-sm-3 control-label">下线人数最大值：</label>
            <div class="col-sm-8">
                <input id="max_line" name="max_line" type="text" autoComplete="off" class="form-control input-sm" placeholder="请输入下线人数最大值" title="请输入下线人数最大值" />
            </div>
        </div>
        <div class="form-group" id="markPass">
            <label class="col-sm-3 control-label">商品折扣<span class="glyphicon glyphicon-asterisk requiredtitle"></span>：</label>
            <div class="col-sm-8">
                <input id="price_rate" name="price_rate" type="text" autoComplete="off" required="" aria-required="true" class="form-control input-sm" placeholder="例如：8.5折请输入8.5" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea class="form-control" id="level_note" name="level_note" rows="3" placeholder="请输入备注信息" title="请输入备注信息"></textarea>
            </div>
        </div>
    </form>
</div>
@section footSection{
    <script>
        var $table, options;
        var levelManager = {
            initPage: function () {
                levelManager.initSelect();
                levelManager.initTable();
                levelManager.bindEvent();
            },
            initTable: function () {
                options = {
                    url: ctx + "/AccountManager/GetUserLevelData",
                    cutHeight: 5,
                    toolbar: "#exampleToolbar",
                    queryParams: function (params) {
                        params = {
                            //页面大小  
                            rows: params.limit,
                            //第几页
                            page: params.offset / params.limit + 1,
                            minScore: $('#minScore').val(),
                            maxScore: $('#maxScore').val(),
                            minLine: $('#minLine').val(),
                            maxLine: $('#maxLine').val(),
                            userLevel:$('#userLevel').val()
                        }
                        return params;
                    },
                    columns: [
                         {
                             radio: true
                         }, {
                             field: '',//第一列序号
                             title: '序号',
                             align: 'center',
                             width: 50,
                             formatter: function (value, row, index) {
                                 return index + 1;
                             }
                         }, {
                             field: 'user_level',
                             title: '用户等级',
                             align: 'center'
                         }, {
                             field: 'min_sale',
                             title: '销售额',
                             align: 'center',
                             formatter: function (value, row, index) {
                                 if (value === 0 && row.max_sale === 0)
                                     return "";
                                 if (value > 0 && row.max_sale <= 0)
                                     return value + '-';
                                 return value + '-' + row.max_sale;
                             }
                         }, {
                             field: 'min_line',
                             title: '下限人数',
                             align: 'center',
                             formatter: function (value, row, index) {
                                 if (value === 0 && row.max_line === 0)
                                     return "";
                                 if (value > 0 && row.max_line <= 0)
                                     return value + '-';
                                 return value + '-' + row.max_line;
                             }
                        }, {
                            field: 'price_rate',
                            title: '折扣率',
                            align: 'center',
                            formatter: function (value) {
                                return value > 0 ? value*10 + '%' : '';
                            }
                        }, {
                             field: 'update_date',
                             title: '最后更新时间',
                             align: 'center'
                         }, {
                             field: 'update_name',
                             title: '最后更新人',
                            align: 'center'
                        }, {
                            field: 'create_date',
                            title: '创建时间',
                            align: 'center'
                        }, {
                            field: 'create_name',
                            title: '创建人',
                            align: 'center'
                        }]
                }
                $table = tableHelper.initTable("exampleTableToolbar", options);
            },
            initSelect: function () {
                $('#user_level').initSelectpicker("UserLevel");
                $('#userLevel').initSelectpicker("UserLevel",null,true);
            },
            //获取选中行数据
            selections: function (isAll) {
                var row = $table.bootstrapTable('getSelections');
                if (!row || row.length === 0) {
                    layer.msg("数据为空或未选中记录行~", { icon: 5 });
                    return false;
                }
                if (isAll)
                    return row;
                if (row.length > 1) {
                    layer.msg("当前操作不支持多选，请选择一行再操作！", { icon: 5 });
                    return false;
                }
                return row[0];
            },
            bindEvent: function () {
                $('#btnSearch').on('click', function () {
                    $table.bootstrapTable('refresh', options);
                });
                $('#userDetail').on('click', function () {
                    var row = levelManager.selections();
                    if (!row)
                        return false;
                    //清空表单
                    $('#sellFormTable').resetHideValidForm();
                    layer.open({
                        title: ['用户等级详细信息', 'font-size:18px;'],
                        type: 1,
                        content: $('#sellForm'),
                        scroll: true,//是否显示滚动条、默认不显示
                        btn: ['确定', '取消'],
                        area: ['600px', '500px'],
                        success: function (li, o) {
                            if (row != null) {
                                $('#sellFormTable')[0].reset();//重置表单
                                $('#sellFormTable').LoadForm(row);//表单填充数据
                            }
                        }
                    });
                });
                $('#userAdd').on('click', function () {
                    levelManager.editForm();
                });
                $('#userEdit').on('click', function () {
                    var row = levelManager.selections();
                    if (!row)
                        return false;
                    levelManager.editForm(row);
                });
                $('#userDel').on('click', function () {
                    var row = levelManager.selections();
                    if (!row)
                        return false;
                    layer.confirm('您确定要删除当前选择用户等级记录？', {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        var index = layer.msg("删除中...", { icon: 6, time: 6000 });
                        $.ajax({
                            type: 'post',
                            url: ctx + '/AccountManager/DelUserLevel/' + row.id,
                            dataType: 'json',
                            async: false,
                            success: function (data) {
                                layer.close(index);
                                if (data.Statu === 0) {
                                    layer.msg("删除成功...");
                                    $table.bootstrapTable('refresh');
                                }
                                else
                                    layer.msg(data.Msg, { icon: 5 });
                            }
                        });
                    }, function () { });
                });
            },
            //编辑、添加
            editForm: function (row) {
                var $title = "添加用户等级信息";
                if (!row)
                    addOrEditUrl = ctx + "/AccountManager/AddUserLevel";//新增
                else {
                    addOrEditUrl = ctx + "/AccountManager/EditUserLevel/" + row.id;//编辑
                    $title = "编辑用户等级信息";
                }
                //清空表单
                $('#sellFormTable').resetHideValidForm();
                //打开编辑
                layer.open({
                    title: [$title, 'font-size:18px;'],
                    type: 1,
                    content: $('#sellForm'),
                    scroll: true,//是否显示滚动条、默认不显示
                    btn: ['确定', '取消'],
                    area: ['600px', '500px'],
                    success: function (li, o) {
                        if (row) {
                            $('#sellFormTable')[0].reset();//重置表单
                            $('#sellFormTable').LoadForm(row);//表单填充数据
                        }
                    },
                    yes: function (li, o) {
                        if ($('#sellFormTable').valid()) {
                            //验证通过
                            $('#sellFormTable').ajaxSubmit({
                                url: addOrEditUrl,
                                type: "post",
                                dataType: "json",
                                beforeSubmit: function (arr, $form, options) {
                                    layer.msg("提交数据~", { icon: 1, time: 5000 });
                                },
                                success: function (result, status, xhr, $form) {
                                    if (result.Statu === 0) {
                                        layer.closeAll();
                                        $table.bootstrapTable('refresh');
                                        layer.msg('保存成功', { icon: 1 });
                                    }
                                    else
                                        layer.alert(result.Msg, { icon: 5 });
                                },
                                error: function (xhr, status, error, $form) {
                                    layer.msg("保存失败~", { icon: 5 });
                                }
                            });
                        }
                    },
                    cancel: function (li, o) {

                    }
                });
            }
        }
        $(function () {
            levelManager.initPage();
        });
    </script>
}
